<template>
  <div>
    <!-- 面包屑导航 -->
    <Breadcrumb :breadcrumbInfo="breadcrumbInfo"></Breadcrumb>
    <el-card>
      <!-- 1.在绘图前我们需要为 ECharts 准备一个定义了高宽的 DOM 容器。 -->
      <div id="main" style="width: 100%; height: 500px"></div>
    </el-card>
  </div>
</template>

<script>
import Breadcrumb from "../../components/Breadcrumb.vue";
// 引入echarts
import * as echarts from "echarts";

export default {
  name: "Report",
  components: { Breadcrumb },
  data() {
    return {
      // 商品列表面包屑导航信息
      breadcrumbInfo: {
        titlePath: [
          { title: "首页", Path: "/Home" },
          { title: "数据统计", Path: "" },
          { title: "数据报表", Path: "" },
        ],
      },

      // 报表数据内容
      originalOptions: {
        title: {
          text: "用户来源",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#E9EEF3",
            },
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            boundaryGap: false,
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
      },

      // 服务器请求得到的数据
      reqOption: {},
    };
  },

  methods: {
    async getReport() {
      const { data: res } = await this.$http.get("reports/type/1");
      if (res.meta.status !== 200) {
        return this.$message.error("获取数据报表失败!");
      }
      console.log(res);
      this.reqOption = res.data;
      this.reqOption = { ...this.reqOption, ...this.originalOptions };
    },
  },

  // dom节点被渲染完毕后执行
  async mounted() {
    // 2. 基于准备好的dom，初始化echarts实例
    let myChart = echarts.init(document.getElementById("main"));

    // 3 .指定图表的配置项和数据
    const { data: res } = await this.$http.get("reports/type/1");
    if (res.meta.status !== 200) {
      return this.$message.error("获取数据报表失败!");
    }

    // 合并数据
    res.data = { ...this.originalOptions, ...res.data };
    // 4.使用刚指定的配置项和数据显示图表。
    myChart.setOption(res.data);
  },
};
</script>

<style>
</style>